<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://feather-cm.googlecode.com/
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

abstract class GenericTagsController extends AppController
{
	protected $restrictedAccess = array('live', 'admin', 'rename', 'erase');
	public    $_templates       = 'tags';
	
	function cloud()
	{
		$this->data = $this->{$this->modelClass}->getCloud();
		$this->load->helper('tagcloud');
		$this->pageTitle = _('Tag cloud');
	}
	
	// admin
	
	function live($q=null)
	{
		if (empty($q))
			exit;
		$this->data = $this->{$this->modelClass}->search($q);
	}
	
	function admin()
	{
		$model = $this->{$this->modelClass};
		if (isset($this->params['url'][$model->tagName]))
			$model->condition($model->tagName, "LIKE {$this->params['url']['tag']}%");
		$this->data = $model->listField($model->tagName, -1);
		
		$this->pageTitle = _('Tags administration');
	}
	
	function rename($old, $new=null)
	{
		if (HTTP::isPost())
		{
			if ($this->{$this->modelClass}->rename($old, $new))
				$this->Session->setFlash(_('Tag has been renamed.'), 'notice');
			else
				$this->Session->setFlash(_('An error occured.'), 'error');
			HTTP::redirect(HTTP::getReferer());
		}
		$this->pageTitle = sprintf(_("Rename tag %s"), $old);
	}
	
	function erase($tag)
	{
		if ($this->{$this->modelClass}->delete())
			$this->Session->setFlash(_('Tag erased.'), 'notice');
		else
			$this->Session->setFlash(_('An error occured.'), 'error');
		HTTP::redirect(HTTP::getReferer());
	}
}
?>